<cfsetting enablecfoutputonly="Yes">

<!--- 
  /**
   * @author  rens@joxx.nl
   * @version 1.0 - 2003.03.07
   */
--->

<cfscript>
  /* ======================================================================= */
  /* This function will determine what the next fuseaction in a PLP will be. */
  /* In order for this function to work correctly, the following             */
  /* requirements should be met:                                             */
  /*                                                                         */
  /*   1. When using a PLP, all forms should be posted to the same           */
  /*      fuseaction (e.g. $WEBROOT$/index.cfm?fuseaction=handleform         */
  /*                                                                         */
  /* The XML file should look like this:                                     */
  /*                                                                         */
  /*   <?xml version="1.0"?>                                                 */
  /*   <plp {id="<plp_id"}>                                                  */
  /*     <fuse id="<fuse/plp location>">                                     */
  /*       <direction id="{forward|backward}">                               */
  /*         <nextfuse id="<next fuseaction in plp>">                        */
  /*           <requirement>expression_to_evaluate</requirement>             */
  /*           <requirement ... />                                           */
  /*           <attribute                                                    */
  /*             id="<attribute_id>"                                         */
  /*             value="<attribute_value>"                                   */
  /*             evaluate="{0|1}"                                            */
  /*           >                                                             */
  /*           <attribute ... />                                             */
  /*         </nextfuse>                                                     */
  /*         <nextfuse ... />                                                */
  /*       <direction/>                                                      */
  /*     </fuse>                                                             */
  /*     <fuse ... />                                                        */
  /*   </plp>                                                                */
  /*                                                                         */
  /* The 'evaluate' attribute in the <attribute/> tag determines if CF       */
  /* should use the evaluate() function to evaluate the given value. If set  */
  /* to 0, the function will just make an "id=value" pair and append it to   */
  /* the resulting string.                                                   */
  /* ======================================================================= */
  
  /**
   * @author  rens@joxx.nl
   * @version  0.1 - 2003.03.07
   * @param    xXMLObject      The XML Objectd that contains the setup of the PLP 
   *                           we're dealing with right now
   * @param    sPLPLocation    The current fuseaction that is being handled in 
   *                           the application
   * @param    bGoingForward   {0|1}
   */
  function generateNextFuse( xXMLObject, sPLPLocation, bGoingForward )
  {
    // determine the direction we're going
    var _sDirection = iif( bGoingForward, de( "forward" ), de( "backward" ));

    // Declare all the variables that we might need
    var _xNextFuse         = "";
    var _sNextFuseQuery    = "//plp/fuse[@id='#sPLPLocation#']/direction[@id='#_sDirection#']/nextfuse";
    var _sNextFuse         = "";
  
    var _xAttribute        = "";
    var _sAttributeQuery   = "";
    var _sAttribute        = "";
    
    var _xRequirement      = "";
    var _sRequirementQuery = "";
    var _sRequirement      = "";
  
    var _sTmpAttribute     = "";
    var _sURLAppend        = "";
    var _nRequirementsMet  = 0;

    // Get all the next fuses for the current location in the plp
    _xNextFuse = XMLSearch( xXMLObject, _sNextFuseQuery );

    for( i = 1; i lte arrayLen( _xNextFuse ); i = i + 1 )
    {
      // Initialize all variables for this next fuse
      _sNextFuse        = _xNextFuse[i].XmlAttributes.id;
      _sURLAppend       = "";
      _nRequirementsMet = 0;
      _sRequirementQuery = "//plp/fuse[@id='#sPLPLocation#']/direction[@id='#_sDirection#']/nextfuse[@id='#_sNextFuse#']/requirement";
      _xRequirement      = XMLSearch( xXMLObject, _sRequirementQuery );
      
      // Count the number of requirements that were met for this fuse
      for( j = 1; j lte arrayLen( _xRequirement ); j = j + 1 )
      {
        if( evaluate( _xRequirement[j].xmltext ))
        {
          _nRequirementsMet = _nRequirementsMet + 1;
        }
      }
      
      // If all requirements are met, fetch the attributes that we might need
      // to pass along with the fuseaction
      if( arrayLen( _xRequirement ) eq _nRequirementsMet )
      {
        _sAttributeQuery = "//plp/fuse[@id='#sPLPLocation#']/direction[@id='#_sDirection#']/nextfuse[@id='#_sNextFuse#']/attribute";
        _xAttribute      = XMLSearch( xXMLObject, _sAttributeQuery );
        
        for( k = 1; k lte arrayLen( _xAttribute ); k = k + 1 )
        {
          if( _xAttribute[k].XmlAttributes.evaluate )
          {
            _sTmpAttribute = _xAttribute[k].XmlAttributes.id & "=" & evaluate( _xAttribute[k].XmlAttributes.value );
          }
          else
          {
            _sTmpAttribute = _xAttribute[k].XmlAttributes.id & "=" & _xAttribute[k].XmlAttributes.value;
          }
          
          _sURLAppend = listAppend( _sURLAppend, _sTmpAttribute, "&" );
        }
        
        if( len( _sURLAppend ))
        {
          _sNextFuse = _sNextFuse & "&" & _sURLAppend;
        }
        
        return( _sNextFuse );
      }
    }
    
    // if no next fuseaction could be found, return to the calling page
    return( sPLPLocation & "&nStatus=#request.status.n_PLP_NO_NEXT_FUSE#" );
  }
  
  /* Copy to a scope where it's visible from everywhere */
  request.udf.generateNextFuse = generateNextFuse;
</cfscript>

<cfsetting enablecfoutputonly="No">